package com.eshop.we.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.eshop.we.book.entity.Book;
import com.eshop.we.mapper.BookMapper;
import com.eshop.we.mapper.UserMapper;
import com.eshop.we.user.api.UserService;
import com.eshop.we.user.dto.UserBooksDto;
import com.eshop.we.user.dto.UserInfoDto;
import com.eshop.we.user.entity.User;
import com.eshop.we.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;


/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author anonymity
 * @since 2018-07-04
 */
@Service(interfaceClass = UserService.class)
@Component
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

  @Autowired
  BookMapper bookMapper;

    @Override
    public UserBooksDto selectUserBooks(Integer userId) {

         UserBooksDto userBooksDto=baseMapper.selectUserAndSchool(userId);
         List<Book> books=bookMapper.selectList(new EntityWrapper<Book>().eq("user_id",userId).eq("status",10));
         Integer count= bookMapper.selectCount(new EntityWrapper<Book>().eq("user_id",userId).eq("status",10));
         userBooksDto.setBooks(books);
         userBooksDto.setBookNum(count);
         return userBooksDto;
    }

    @Override
    public Integer updateByOpenId(User user) {
        return baseMapper.updateByOpenId(user);
    }

    @Override
    public Page selectUserPage(Page<UserInfoDto> page, String name,String mobile, String schoolName, String startDate, String endDate) throws  Exception{

        Long start= startDate!=null?DateUtil.str2Date(startDate+ " 00:00:00").getTime():null;
        Long end= endDate!=null?DateUtil.str2Date(endDate+" 23:59:59").getTime():null;
        List<UserInfoDto> list=baseMapper.selectUserPage(page,name,mobile,schoolName,start,end);
        page.setRecords(list);
        return page;
    }
    @Override
    public UserInfoDto getUserInfo(Integer userId) {
        return baseMapper.getUserInfo(userId);
    }
}
